<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
	<form action="#">
		<input type="text" id="username1" value="请输入用户名">
		<input type="text" id="username2">
		<input type="submit">
	</form>
	<script>
		//var result;
		var username1 = document.getElementById('username1')
		username1.addEventListener('copy', function (event) {

			var data = event.clipboardData || window.clipboardData;
			console.log(data);


			console.log('这是一个复制操作');
			var value = username1.value;
			var result = value.substring(username1.selectionStart, username1.selectionEnd)
			console.log(result);

			data.setData('text', result)



		})
		var username2 = document.getElementById('username2')
		username2.addEventListener('paste', function (event) {

			var data = event.clipboardData || window.clipboardData;
			var result = data.getData('text')

			event.preventDefault();
			if (result === '用户名') {
				result = '***'
			}
			username2.value = result


		})

	</script>
</body>

</html>